﻿using System.Data.SqlClient;
using GeneralLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GeneralLibs.Lists;
using System.Collections;
using Security;

namespace Portal.DanhMuc
{
    public partial class FrmDanhMucDiaBan : FrmBaseListForm
    {
        public FrmDanhMucDiaBan()
        {
            InitializeComponent();
        }

        private void FrmDanhMucDiaBan_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
            gridDSDoiTuong.Visible = false;
            tlvDanhMucDiaBan.OptionsBehavior.Editable = false;
            tlvDanhMucDiaBan.ParentFieldName = "MaDiaBanChaID";
            tlvDanhMucDiaBan.ExpandAll();
        }
        void Instance_Change()
        {
            LoadData();
        }
        public override void LoadData()
        {
            try
            {
                tlvDanhMucDiaBan.DataSource = DanhMucDiaBan.Instance.DataView;
                InitGridColumnNews();
            }
            catch(Exception ex)
            {
            }
        }
        public void InitGridColumnNews()
        {
            foreach (DevExpress.XtraTreeList.Columns.TreeListColumn column in tlvDanhMucDiaBan.Columns)
            {
                column.Visible = false;
            }

            tlvDanhMucDiaBan.Columns["MaDiaBan"].Caption = "Mã";
            tlvDanhMucDiaBan.Columns["MaDiaBan"].Visible = true;
            tlvDanhMucDiaBan.Columns["MaDiaBan"].VisibleIndex = 1;
            tlvDanhMucDiaBan.Columns["TenDiaBan"].Caption = "Tên";
            tlvDanhMucDiaBan.Columns["TenDiaBan"].Visible = true;
            tlvDanhMucDiaBan.Columns["TenDiaBan"].VisibleIndex = 2;

            tlvDanhMucDiaBan.Columns["TenTat"].Caption = "Tên tắt";
            tlvDanhMucDiaBan.Columns["TenTat"].Visible = true;
            tlvDanhMucDiaBan.Columns["TenTat"].VisibleIndex = 3;
        }

        private void TlvDanhMucDiaBan_DoubleClick(object sender, EventArgs e)
        {
            Edit();        
        }
        public override void CreateNew()
        {
            try
            {
                FrmEditDiaBan frm = new FrmEditDiaBan(null);
                frm.ShowDialog();
            }
            catch(Exception ex)
            {
            }
        }
        protected override void Edit()
        {
            try
            {
                Guid id = (Guid)tlvDanhMucDiaBan.Selection[0]["ID"];
                FrmEditDiaBan frm = new FrmEditDiaBan(DanhMucDiaBan.Instance.GetByID(id));
                frm.ShowDialog();
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        protected override void Delete()
        {
            try
            {
                Guid id = (Guid)tlvDanhMucDiaBan.Selection[0]["ID"];
                DiaBan deletedObj = DanhMucDiaBan.Instance.GetByProperty("ID", id);
                if (deletedObj != null)
                {
                    if (MessageBox.Show("Bạn có thực sự muốn xóa địa bàn?", "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.No)
                        return;
                    else
                        deletedObj.DeleteFromDB();
                }
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if(ex.Message.StartsWith("Lỗi khóa ngoại"))
                {
                    throw new Exception("Địa bàn này đã được sử dụng ở các danh mục khác.\nKhông thể xóa địa bàn này.");
                }
                else if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
